package com.yuexiao.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yuexiao.entity.DishDto;
import com.yuexiao.entity.ResultInfo;
import com.yuexiao.pojo.Dish;
import com.yuexiao.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @ClassName:DishController
 * @Auther: YUEXIAO
 * @Description: 菜品操作控制器
 * @Date: 2022/8/11 20:16
 * @Version: v1.0
 */
@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Reference
    private DishService dishService;


    /**
     * 根据条件分页查询菜品列表
     * @param page int 当前页
     * @param pageSize int 每页显示条数
     * @param name String 菜品名称
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/page")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo page(int page,int pageSize,String name){
        log.info("page = {},pageSize = {},name = {}" ,page,pageSize,name);
        return ResultInfo.success(dishService.getDishByPage(page,pageSize,name));
    }

    /**
     * 新增菜品
     * @param dishDto 封装的接收菜品实体类
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/addDish")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo addDish(@RequestBody DishDto dishDto){
        log.info(dishDto.toString());
        return ResultInfo.success(dishService.insertWithFlovor(dishDto));
    }

    /**
     * 根据菜品主键查询菜品数据
     * @param id Long 菜品id
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/queryDishById/{id}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo queryDishById(@PathVariable Long id){
        return ResultInfo.success(dishService.getDishById(id));
    }


    /**
     * 更新菜品信息
     * @param dishDto DishDto 封装的菜品实体对象
     * @return ResultInfo 返回结果对象
     */
    @PutMapping("/editDish")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo editDish(@RequestBody DishDto dishDto){
        return ResultInfo.success(dishService.updateDish(dishDto));
    }


    /**
     * 修改菜品上架状态
     * @param status int 要修改的状态
     * @param ids Long[] 主键id数组
     * @return ResultInfo 返回结果对象
     */
    @PostMapping("/dishStatusByStatus/{status}")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo dishStatusByStatus(@PathVariable int status,Long[] ids){
        log.info("状态:{}",status);
        log.info("ids长度:{}",ids.length);
        return ResultInfo.success(dishService.updatedDishStatusByIds(status, ids));
    }


    /**
     * 删除菜品信息
     * @param ids Long[] 主键id数组
     * @return ResultInfo 返回结果对象
     */
    @DeleteMapping("/deleteDish")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo deleteDish(Long[] ids){
        return ResultInfo.success(dishService.deleteDishByIds(ids));
    }


    /**
     * 根据分类id查询菜品列表
     * @param categoryId Long 分类id
     * @return ResultInfo 返回对象列表
     */
    @GetMapping("/queryDishList")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo queryDishList(Long categoryId){
        log.info("分类id:{}",categoryId);
        return ResultInfo.success(dishService.getDishListByCategoryId(categoryId));
    }

    /**
     * 根据菜品名模糊查询菜品列表
     * @param name String 菜品名
     * @return ResultInfo 返回结果对象
     */
    @GetMapping("/queryDishListByName")
    @PreAuthorize("hasAnyRole('ROLE_ADMIN','ROLE_USER')")
    public ResultInfo queryDishListByName(String name){
        return ResultInfo.success(dishService.getDishListByName(name));
    }

}
